package com.course.smart_campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.course.smart_campus.entity.DeliverOrder;
import com.course.smart_campus.entity.User;
import com.course.smart_campus.service.DeliverOrderService;
import com.course.smart_campus.mapper.DeliverOrderMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author feng-shu-sheng
 * @description 针对表【deliver_order】的数据库操作Service实现
 * @createDate 2023-11-07 19:42:44
 */
@Service
public class DeliverOrderServiceImpl extends ServiceImpl<DeliverOrderMapper, DeliverOrder>
        implements DeliverOrderService {
    @Resource
    private DeliverOrderMapper deliverOrderMapper;

    @Override
    public List<DeliverOrder> getByClientId(int clientId) {
        LambdaQueryWrapper<DeliverOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeliverOrder::getClientId, clientId);
        return deliverOrderMapper.selectList(wrapper);
    }

    @Override
    public List<DeliverOrder> getByAcceptId(int accepterId) {
        LambdaQueryWrapper<DeliverOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeliverOrder::getAccepterId, accepterId);
        return deliverOrderMapper.selectList(wrapper);
    }

    @Override
    public boolean updateStatus(int id,int status) {
        DeliverOrder deliverOrder = deliverOrderMapper.selectById(id);
        deliverOrder.setStatus(status);
        if (deliverOrderMapper.updateById(deliverOrder) > 0) {
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean accept(int acceptId, int id) {
        DeliverOrder deliverOrder = new DeliverOrder();
        deliverOrder.setAccepterId(acceptId);
        deliverOrder.setStatus(1);
        deliverOrder.setId(id);
        if (deliverOrderMapper.updateById(deliverOrder) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean cancelAccept(int acceptId, int id) {
        DeliverOrder deliverOrder = deliverOrderMapper.selectById(id);
        if (deliverOrder.getAccepterId() == acceptId) {
            deliverOrder.setAccepterId(null);
            deliverOrder.setStatus(0);
            if (deliverOrderMapper.updateById(deliverOrder) > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean finish(int id) {
        DeliverOrder deliverOrder = new DeliverOrder();
        deliverOrder.setId(id);
        deliverOrder.setStatus(2);
        if (deliverOrderMapper.updateById(deliverOrder) > 0) {
            return true;
        }
        return false;
    }


}




